﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.IO;
using System.Diagnostics;

namespace BackLib
{
        struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        public enum Format
        {
            Jpeg,
            HDP
        }

        public class DirectCapture
        {
            IntPtr Handle;
            RECT Bounds = new RECT();

            [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

            public DirectCapture(IntPtr Handle)
            {
                // Get the Screen Size
                this.Handle = Handle;
            }

            public DirectCapture(string GameName)
            {
                Process[] Apps = Process.GetProcesses();
                for (int i = 0; i < Apps.Length; i++)
                {
                    if (Apps[i].MainWindowTitle == GameName)
                        Handle = Apps[i].MainWindowHandle;
                }
                if (Handle.ToInt32() == 0)
                    MessageBox.Show("Could not Find the Game's Process!");
            }

            public Image ScreenCap(Format format, int Quality)
            {
                // Update
                GetWindowRect(Handle, ref Bounds);

                // Get the Screen Cap
                Bitmap ScreenCap = new Bitmap(Bounds.right - Bounds.left, Bounds.bottom - Bounds.top, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                Graphics g = Graphics.FromImage(ScreenCap);
                g.CopyFromScreen(Bounds.left, Bounds.top, 0, 0, new Size(Bounds.right - Bounds.left, Bounds.bottom - Bounds.top));
                Image Image = Image.FromHbitmap(ScreenCap.GetHbitmap());

                // Format and return
                if (format == Format.Jpeg)
                    return Image.FromHbitmap(ScreenCap.GetHbitmap());
                else
                {
                    // Write the Image to a Stream
                    MemoryStream ms = new MemoryStream();
                    Image.Save(ms, ImageFormat.Jpeg);
                    ms.Position = 0;

                    // Open and Decode
                    BitmapDecoder bdFile = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);

                    // All the magic done by WmpBitmapEncoder
                    WmpBitmapEncoder wbeFile = new WmpBitmapEncoder();
                    wbeFile.ImageQualityLevel = (float)(Quality * 1.0);
                    wbeFile.Frames.Add(bdFile.Frames[0]);

                    // Write to a new Stream
                    ms = new MemoryStream();
                    wbeFile.Save(ms);

                    // Return
                    return Image.FromStream(ms);
                }
            }

            public byte[] GetBytes(Format format, int Quality)
            {
                // Update
                GetWindowRect(Handle, ref Bounds);

                // Get the Screen Cap
                Bitmap ScreenCap = new Bitmap(Bounds.right - Bounds.left, Bounds.bottom - Bounds.top, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                Graphics g = Graphics.FromImage(ScreenCap);
                g.CopyFromScreen(Bounds.left, Bounds.top, 0, 0, new Size(Bounds.right - Bounds.left, Bounds.bottom - Bounds.top));
                Image Image = Image.FromHbitmap(ScreenCap.GetHbitmap());

                // Format and return
                if (format == Format.Jpeg)
                {
                    Image i = Image.FromHbitmap(ScreenCap.GetHbitmap());
                    MemoryStream ms = new MemoryStream();
                    i.Save(ms, ImageFormat.Jpeg);
                    return ms.ToArray();
                }
                else
                {
                    // Write the Image to a Stream
                    MemoryStream ms = new MemoryStream();
                    Image.Save(ms, ImageFormat.Jpeg);
                    ms.Position = 0;

                    // Open and Decode
                    BitmapDecoder bdFile = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);

                    // All the magic done by WmpBitmapEncoder
                    WmpBitmapEncoder wbeFile = new WmpBitmapEncoder();
                    wbeFile.ImageQualityLevel = (float)(Quality * 1.0);
                    wbeFile.Frames.Add(bdFile.Frames[0]);

                    // Write to a new Stream
                    ms = new MemoryStream();
                    wbeFile.Save(ms);

                    // Return
                    return ms.ToArray();
                }
            }
        }
    }
